package org.lex.dbq.view.sql.result;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.lex.dbq.model.export.HtmlExporter;
import org.lex.dbq.util.Constants;
import org.lex.dbq.util.DialogUtils;

public class ExportHtmlAction extends ResultSetAction {
	private final IResultSetViewer viewer;

	public ExportHtmlAction(IResultSetViewer viewer) {
		this.viewer = viewer;
		this.setText("Export HTML");
	}

	public void run() {
		File file = DialogUtils.chooseFile(getShell(), SWT.SAVE, "*.html");
		log.info("export file: " + file);
		if (null == file) {
			return;
		}
		HtmlExporter export = new HtmlExporter();
		try {
			ResultSet rs = viewer.getResultSet().execute();
			export.export(rs, file, Constants.CHARSET);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
